package com.tuishan.myapplication.ViewModels;

import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.paging.PositionalDataSource;

import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.SPUtils;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.tuishan.myapplication.Base.BaseContest;
import com.tuishan.myapplication.Model.PageNumModel;
import com.tuishan.myapplication.Model.UserLogModel;
import com.tuishan.myapplication.Servers.BaseCallback;
import com.tuishan.myapplication.Servers.OkHttpHelper;
import com.tuishan.myapplication.Servers.UrlRouter;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;
import java.util.WeakHashMap;

import okhttp3.Request;
import okhttp3.Response;

/**
 * <pre>
 *     time   : 2021/04/17
 *     desc   :
 *     version: 1.0
 * </pre>
 */

public class UserLogDataSource extends PositionalDataSource<UserLogModel> {

    int Index;
    int indextype;

    public UserLogDataSource() {
        this.Index = 1;
        this.indextype = 0;
    }

    public UserLogDataSource(int logType) {
        this.Index = 1;
        this.indextype = logType;
    }

    @Override
    public void loadInitial(@NonNull LoadInitialParams params, @NonNull final LoadInitialCallback<UserLogModel> callback) {
        GetLog(Index, 10, indextype, (value, totalCount) -> callback.onResult(value, 0, totalCount));
    }

    @Override
    public void loadRange(@NonNull LoadRangeParams params, @NonNull final LoadRangeCallback<UserLogModel> callback) {
        Index++;
        GetLog(Index, params.loadSize, indextype, (value, totalCount) -> callback.onResult(value));

    }

    /**
     * 获取操作日志
     */
    private void GetLog(int pageNum, int pageSize, int logType, final Callback callback) {
        WeakHashMap<String, Object> objectMap = new WeakHashMap<>();
        objectMap.put("pageNum", pageNum);
        objectMap.put("pageSize", pageSize);
        objectMap.put("logType", logType);
        OkHttpHelper mHttpHelper = OkHttpHelper.getInstance();
        mHttpHelper.post(UrlRouter.UserLogUrl, String.class, objectMap, new BaseCallback() {
            @Override
            public void onFailure(Request request, Exception e) {

            }

            @Override
            public void onSuccess(Response response, Object o) {
                Gson gson = new Gson();
                PageNumModel<UserLogModel> modelPageNumModel = gson.fromJson(o.toString(),
                        new TypeToken<PageNumModel<UserLogModel>>() {
                        }.getType());
                for (UserLogModel model : modelPageNumModel.getList()) {
                    model.setPageNum(modelPageNumModel.getPageNum());
                }
                if (modelPageNumModel.getList().size() != 0) {
                    callback.onResult(modelPageNumModel.getList(), modelPageNumModel.getTotal());
                }
            }

            @Override
            public void onFalse(Response response, String err_msg) {

            }

            @Override
            public void onError(Response response, int errorCode, Exception e) {

            }
        });
    }

    private interface Callback {

        void onResult(List<UserLogModel> value, int totalCount);
    }
}
